package it.francescogabbrielli.android.solidalapp;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class Report {

	private String message;

	private StringBuilder logs;

	public Report(String message) {
		this.message = message;
		try {
			Process process = Runtime.getRuntime().exec("logcat -d");
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(process.getInputStream()));
			logs = new StringBuilder();
			String line = "";
			while ((line = bufferedReader.readLine()) != null)
				logs.append(line+"\n");
		} catch (Exception e) {
			Log.e(SolidalApp.TAG, "Cannot report issue", e);
		}
	}

	public void send(Context context) {
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");
		String version = "?";
		try {
			version = context.getPackageManager()
					.getPackageInfo(getClass().getPackage().getName(), 0).versionName;
		} catch(Exception e) {
			
		}
		intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"solidalapp@francescogabbrielli.it"});
		intent.putExtra(Intent.EXTRA_SUBJECT, "[Report v"+version+"] "+message);
		intent.putExtra(Intent.EXTRA_TEXT, logs.toString());
		context.startActivity(Intent.createChooser(
				intent, context.getString(R.string.pref_report_chooser)));
	}

}
